<?php


namespace App\Admin\Controllers\ApiDoc;

use App\Constant\ContentType;
use App\Http\Controllers\Controller;
use App\Model\Repository\ApiRepository;
use App\Model\Repository\ApiResponseBodyRepository;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Michelf\Markdown;

class ApiResponseBodyController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Index');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(ApiResponseBodyRepository::findOrFail($id), function (Show $show) {

                $show->field('response_id', '响应参数id');
                $show->api('所属接口', function (Show $show) {
                    $show->setResource('/admin/apidoc/api');
                    $show->field('api_name');
                });
                $show->field('body', '返回参数')->as(function ($body) {
                    return sprintf("<pre>%s</pre>", Markdown::defaultTransform($body));
                });
                $show->field('demo', '返回参数demo')->as(function ($demo) {
                    return sprintf("<pre>%s</pre>", Markdown::defaultTransform($demo));
                });
                $show->field('content_type', '响应数据类型');
                $show->field('remark', '备注');
                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');

            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Edit');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ApiResponseBodyRepository::class, function (Grid $grid) {

            $grid->column('response_id', '响应参数id');
            $grid->column('api.api_name', '所属接口');
//            $grid->column('body', '返回参数');
//            $grid->column('demo', '返回参数demo');
            $grid->column('content_type', '响应数据类型');
            $grid->column('remark', '备注');

            $grid->column('created_at', '创建时间');
//            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ApiResponseBodyRepository::class, function (Form $form) {

            $form->display('response_id', '响应参数id');
            $form->select('api_id', '所属接口')->options(ApiRepository::getApiMap())->rules('required');
            $form->editor('body', '返回参数')->rules('required');
            $form->editor('demo', '返回参数demo')->rules('required');
            $form->select('content_type', '响应数据类型')->options(ContentType::getSelectOptions())->default('JSON')->rules('required');
            $form->text('remark', '备注')->default('暂无')->rules('required');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
        });
    }
}